package com.haust.su.ghyy.hospital.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.haust.su.ghyy.hospital.repository.DepartmentRepository;
import com.haust.su.ghyy.hospital.service.DepartmentService;
import com.haust.su.ghyy.model.hosp.Department;
import com.haust.su.ghyy.vo.hosp.DepartmentQueryVo;
import com.haust.su.ghyy.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author haustsusu
 * @create 2023-04-27-10:42
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    //调用service方法,添加医院接口
    @Override
    public void save(Map<String, Object> parammap) {
        //parammap转换为department对象
        String parammapString = JSONObject.toJSONString(parammap);
        Department department = JSONObject.parseObject(parammapString, Department.class);

        //判断科室信息是否存在

        //根据医院编号和科室编号进行查询
        Department departmentExist
                = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());

        //判断是否有该科室
        //假如有科室
        if(departmentExist!=null){
            departmentExist.setUpdateTime(new Date());
            departmentExist.setIsDeleted(0);
            departmentRepository.save(departmentExist);
        }else{//假如没有
            department.setUpdateTime(new Date());
            department.setCreateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        //创建pageable对象,设置当前页与每页数量
        //0是第一页
        Pageable pageable= PageRequest.of(page-1,limit);

        //创建department对象
        Department department=new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);

        //创建example对象
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)//进行模糊查询
                .withIgnoreCase(true);//查询中忽略大小写
        Example<Department> example=Example.of(department,matcher);

        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }


    //删除科室接口
    @Override
    public void remove(String hoscode, String depcode) {
        //根据医院的编号与科室编号查询
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department!=null){
            department.setIsDeleted(1);
            //调用删除方法,此处使用的是逻辑删除
            departmentRepository.save(department);
            //此处是真实的删除
            //departmentRepository.deleteById(department.getId());
        }

    }


    //根据医院的编号,查询医院的所有科室列表
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //创建list集合,最后用于封装
        List<DepartmentVo> list=new ArrayList<>();

        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example<Department> example = Example.of(departmentQuery);
        //得到所有的科室信息   所有科室信息在departmentList
        List<Department> departmentList = departmentRepository.findAll(example);

        Map<String, List<Department>> departmentMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));

        //遍历map集合 departmentMap
        for (Map.Entry<String,List<Department>> entry:departmentMap.entrySet()){
            //大科室编号
            String bigcode=entry.getKey();

            //大科室编号对应的全局数据
            List<Department> departmentsListes = entry.getValue();

            //封装大科室
            DepartmentVo departmentVoEntry=new DepartmentVo();
            departmentVoEntry.setDepcode(bigcode);
            departmentVoEntry.setDepname(departmentsListes.get(0).getBigname());


            //封装小科室
            List<DepartmentVo> children=new ArrayList<>();
            for (Department department:departmentsListes){
                DepartmentVo departmentVo2=new DepartmentVo();
                departmentVo2.setDepcode(department.getDepcode());
                departmentVo2.setDepname(department.getDepname());
                children.add(departmentVo2);
            }


            //把小科室list集合放入到大科室中children中
            departmentVoEntry.setChildren(children);

            //最终封装在list中
            list.add(departmentVoEntry);




        }

        return list;
    }


    //根据医院编号和科室编号查询出科室的名称
    @Override
    public String getDeptName(String hoscode, String depcode) {
        //根据医院编号和科室编号查询出科室对象
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        //获取科室对象的名称
        if (department!=null){
            return department.getDepname();
        }
        return null;
    }


    //根据医院编号,科室编号,查询科室的对象
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return department;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        String depname = department.getDepname();
        return depname;
    }

}
